package cn.hanyx.coder.core.concurrent.thread.demo03;

import java.util.Scanner;

/**
 * DaemonThread
 *
 * @author hanyx
 * @date 2020/09/22
 */
public class DaemonThread {
    public static void main(String[] args) {
        Thread thread = new Thread(new DaemonRunner());
        thread.setDaemon(true);
        thread.start();
        System.out.println("isDaemon? = " + thread.isDaemon());
        Scanner scanner = new Scanner(System.in);
        scanner.next();
        Runtime.getRuntime().addShutdownHook(new Thread() {
            @Override
            public void run() {
                System.out.println("JVM Exit!");
            }
        });
    }
}

class DaemonRunner implements Runnable {
    @Override
    public void run() {
        while (true) {
            for (int i = 1; i <= 100; i++) {
                System.out.println("daemon thread:"+i);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
